﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using AMing.Extension.Net40;

namespace MusicForMvc3.Method.Cache
{
    public static class CacheBaseEx
    {
        /// <summary>
        /// 经过Cache的GetData
        /// </summary>
        /// <param name="cachebase"></param>
        /// <param name="httpContext"></param>
        /// <returns></returns>
        public static string Cache_GetData<ClassType, ReturnType>(this CacheBase<ClassType, ReturnType> cachebase, HttpContextBase httpContext)
            where ReturnType : class
        {
            string cachekey = "Cache_GetData_{0}".ToFromat(CacheHelper.GetCacheKey<ClassType>(cachebase));

            return CacheHelper.GetCacheData<string>(cachekey, httpContext, () =>
            {
                return cachebase.GetData();
            });
        }
        /// <summary>
        /// 经过Cache的GetObject
        /// </summary>
        /// <param name="cachebase"></param>
        /// <param name="httpContext"></param>
        /// <returns></returns>
        public static ReturnType Cache_GetObject<ClassType, ReturnType>(this CacheBase<ClassType, ReturnType> cachebase, HttpContextBase httpContext)
            where ReturnType : class
        {
            string cachekey = "Cache_GetObject_{0}".ToFromat(CacheHelper.GetCacheKey<ClassType>(cachebase));

            return CacheHelper.GetCacheData<ReturnType>(cachekey, httpContext, () =>
            {
                return cachebase.GetObject();
            });
        }

        /// <summary>
        /// 清空包含指定key的缓存
        /// </summary>
        /// <typeparam name="ThisType"></typeparam>
        /// <typeparam name="ReturnType"></typeparam>
        /// <param name="thisDataModel"></param>
        /// <param name="httpContext"></param>
        public static void DeleteCache<ClassType, ReturnType>(this CacheBase<ClassType, ReturnType> cachebase, HttpContextBase httpContext)
        {
            string cachekey = CacheHelper.GetApiCacheKey<ClassType>(cachebase);

            CacheHelper.ClearCache(cachekey, httpContext);
        }
    }
}